package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad Requisitos.<br>
 */
public class AccionesRequisitos extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción RequisitosGrupoCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward RequisitosGrupoCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "requisitosGrupoCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            FRequisitos fFRequisitos = (FRequisitos)sess.getAttribute("FRequisitos");
            if (fFRequisitos==null) {
                fFRequisitos = new FRequisitos();
                sess.setAttribute("FRequisitos", fFRequisitos);
            } else {
                fFRequisitos.reset(mapping, request);
            }
             
            //Feed for combo listAsignatura.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(codigo, idAsignatura) " + 
               "from Asignatura").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListAsignatura(list);
            }
            //Feed for combo listGrupo.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idGrupo) " + 
               "from Grupo").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListGrupo(list);
            }

            fFRequisitos.setGrupo(Long.parseLong((String)request.getParameter("idGrupo")));
            
            java.util.List listA = (java.util.List)s.createQuery("from Asignatura").list();
            request.setAttribute("L_Asignatura", listA);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción RequisitosI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward RequisitosI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "requisitos";

        String salida = SALIDA_0;
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idRequisito");
            Long idRequisito = (id==null) ? 
                (Long)request.getSession().getAttribute("idRequisito") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FRequisitos fFRequisitos = (FRequisitos)sess.getAttribute("FRequisitos");
            if (fFRequisitos==null) {
                fFRequisitos = new FRequisitos();
                sess.setAttribute("FRequisitos", fFRequisitos);
            } else {
                fFRequisitos.reset(mapping, request);
            }
            Requisitos requisitos = (Requisitos)s.load(Requisitos.class, idRequisito);
            fFRequisitos.setIdRequisito(requisitos.getIdRequisito());

            fFRequisitos.setGrupo(requisitos.getGrupo().getIdGrupo());

            if (requisitos.getAsignatura()!=null) {
                fFRequisitos.setAsignatura(requisitos.getAsignatura().getIdAsignatura());
            } else {
                fFRequisitos.setAsignatura(0);
            }
            fFRequisitos.setRequisitos(requisitos.getRequisitos());

            //Feed for combo listAsignatura.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(codigo, idAsignatura) " + 
               "from Asignatura").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListAsignatura(list);
            }
            //Feed for combo listGrupo.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idGrupo) " + 
               "from Grupo").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListGrupo(list);
            }
            
            java.util.List listA = (java.util.List)s.createQuery("from Asignatura").list();
            request.setAttribute("L_Asignatura", listA);

            request.getSession().setAttribute("idGrupo", fFRequisitos.getGrupo());

            request.getSession().setAttribute("idRequisito", idRequisito);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción RequisitosAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward RequisitosAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "requisitos";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FRequisitos fFRequisitos = (FRequisitos)form;

            Requisitos requisitos = (Requisitos)s.load(Requisitos.class, new Long(fFRequisitos.getIdRequisito()));
            if (fFRequisitos.getAsignatura()==0) requisitos.setAsignatura(null);
            else if ((requisitos.getAsignatura()==null) ||
                    (requisitos.getAsignatura().getIdAsignatura()!=fFRequisitos.getAsignatura())) {
                Asignatura obj = (Asignatura)s.load(Asignatura.class, fFRequisitos.getAsignatura());
                requisitos.setAsignatura(obj);
            }
            requisitos.setRequisitos(fFRequisitos.getRequisitos());


            //Feed for combo listAsignatura.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(codigo, idAsignatura) " + 
               "from Asignatura").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListAsignatura(list);
            }
            //Feed for combo listGrupo.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idGrupo) " + 
               "from Grupo").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListGrupo(list);
            }
            
            java.util.List listA = (java.util.List)s.createQuery("from Asignatura").list();
            request.setAttribute("L_Asignatura", listA);

            request.getSession().setAttribute("idGrupo", fFRequisitos.getGrupo());

            request.getSession().setAttribute("idRequisito", requisitos.getIdRequisito());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción RequisitosElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward RequisitosElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "GrupoI";
        final String SALIDA_1 = "requisitos";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idRequisito");
            Long idRequisito = (id==null) ? 
                (Long)request.getSession().getAttribute("idRequisito") :
                new Long(id);
            Requisitos requisitos = (Requisitos)s.load(Requisitos.class, idRequisito);

            long idGrupo = requisitos.getGrupo().getIdGrupo();
            requisitos.getGrupo().removeFromRequisitos(requisitos);

            try {
                s.flush();
                request.getSession().setAttribute("idRequisito", idRequisito);

                request.getSession().setAttribute("idGrupo", idGrupo);

            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción RequisitosCrear.
     * Registro de la creación de una requisitos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward RequisitosCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "requisitos";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FRequisitos fFRequisitos = (FRequisitos)form;

            Requisitos requisitos = new Requisitos();
            if (fFRequisitos.getAsignatura()==0) requisitos.setAsignatura(null);
            else if ((requisitos.getAsignatura()==null) ||
                    (requisitos.getAsignatura().getIdAsignatura()!=fFRequisitos.getAsignatura())) {
                Asignatura obj = (Asignatura)s.load(Asignatura.class, fFRequisitos.getAsignatura());
                requisitos.setAsignatura(obj);
            }
            requisitos.setRequisitos(fFRequisitos.getRequisitos());


            {Grupo obj = (Grupo)s.load(Grupo.class, new Long(fFRequisitos.getGrupo()));
             obj.addToRequisitos(requisitos);
            }

            s.flush();
            fFRequisitos.setIdRequisito(requisitos.getIdRequisito());
            //Feed for combo listAsignatura.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(codigo, idAsignatura) " + 
               "from Asignatura").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListAsignatura(list);
            }
            //Feed for combo listGrupo.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idGrupo) " + 
               "from Grupo").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFRequisitos.setListGrupo(list);
            }
            
            //fFRequisitos.setGrupo(requisitos.getGrupo().getIdGrupo());
            request.getSession().setAttribute("idGrupo", fFRequisitos.getGrupo());

            request.getSession().setAttribute("idRequisito", requisitos.getIdRequisito());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }



}